package englishBusiness;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.GetRequestInputStream;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Objects;

/**
 * Created by yangxianming on 2020/6/10.
 * 记录错误
 */
public class RecordMistake implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        //读取请求json
        JSONObject jsonRequest = GetRequestInputStream.getJson(request);
        ReturnJson returnJson = new ReturnJson();

        //从记录错误数据
        if (Objects.equals("mistakeWords", jsonRequest.getString("action"))){
            //组装sql语句
            //UPDATE words SET forget = "2" WHERE word = "enthusiasm"
            ArrayList<String> updateExc = new ArrayList<String>();
            SqlDispatch sqlDispatch = new SqlDispatch(myConfig, "");
            JSONArray mistakeList = jsonRequest.getJSONArray("body");
            for (Object mistake: mistakeList){
                JSONArray one = (JSONArray)mistake;
                String forgetCount = one.getString(2);
                String forgetWord = one.getString(0);
                String sqlWords = "UPDATE words SET forget = \"" + forgetCount + "\" WHERE word = \"" + forgetWord + "\"";
                SqlReturnModel sqlReturnModel = sqlDispatch.updateEfficiency(sqlWords);
                if (!sqlReturnModel.getIsSuccess()){
                    updateExc.add(forgetWord);
                }
            }
            if (updateExc.size() == 0){
                returnJson.setResult("Error data saved successfully");
            }else {
                returnJson.setResult("Error data saved failed:" + updateExc);
            }
            returnJson.setApiStatus(1);

        }
        //拿到词组
        if (Objects.equals("getPhrase", jsonRequest.getString("action"))){
            //等开始背词组了再开始记录
        }



        return returnJson.toString();
    }
}
